package com.example.administrator.wallet.adapter;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.utils.SharedPrefsUtil;

import java.util.List;

/**
 * Effect:
 * <p>
 * Author:  CaiYapeng
 * <p>
 * Time  :  2018/7/10.
 */

public class AssetListAdapter extends BaseAdapter {
    private Activity context;
    private List<MainBean.DataBean.ListBean> datas;
    private OnClick click;

    public AssetListAdapter(FragmentActivity activity, List<MainBean.DataBean.ListBean> dataBeanList) {
        this.context = activity;
        this.datas = dataBeanList;
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.asset_list_item, null);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.icon = (ImageView) view.findViewById(R.id.icon);
        holder.name = (TextView) view.findViewById(R.id.name);
        holder.counts = (TextView) view.findViewById(R.id.counts);
        holder.price = (TextView) view.findViewById(R.id.price);
        holder.linear = (LinearLayout) view.findViewById(R.id.linear);

        holder.name.setText(datas.get(i).getCoin_symbol());
        holder.counts.setText(datas.get(i).getBalance());
        holder.price.setText("≈￥" + datas.get(i).getMoney());
        Glide.with(context).load(datas.get(i).getIcon()).into(holder.icon);
        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click.Click(datas.get(i));
            }
        });


        SharedPrefsUtil.putValue(context, "wallet", datas.get(i).getCoin_symbol()+"addr", datas.get(i).getAddr());

        return view;
    }

    public class ViewHolder {
        private ImageView icon;
        private TextView counts;
        private TextView name;
        private TextView price;
        private LinearLayout linear;
    }

    public interface OnClick {
        void Click(MainBean.DataBean.ListBean dataBean);
    }

    public void getClickPst(OnClick click) {
        this.click = click;
    }
}
